package com.jcnet.bizaia.commCategory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.DiscussSearchActivity;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.VodSearchActivity;
import com.jcnet.bizaia.bean.BusinessCategoryBean;
import com.jcnet.bizaia.bean.CatgoryBean;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.jcnet.bizaia.xstream.ACache;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class BusinessCategoryList {
	
	


	/**
	 * 得到全部行业分类
	 */

	private ACache aCache;
	
	private Context context;
	
	private RequestParams params;
	
	private List<BusinessCategoryBean> catgoryBeans;
	
	private static  String CATEGORY_HTTP_REQUEST = App.HTTP_PATH+"ind_showIndustryList.action?";
	
	private ProgressHUD progressHUD;

	
	private String requestString = null;
	
	private int type = 400;
	
	private String userMethord ;
	
	public BusinessCategoryList(Context context,RequestParams params) {
		super();
		this.context = context;
		this.params = params;
		aCache = ACache.get(context);
		progressHUD = ProgressHUD.show(context, context.getString(R.string.dataloding), false, true, null);
		catgoryBeans = new ArrayList<BusinessCategoryBean>();
	}
	/**
	 * 
	 * @param context
	 * @param params    http 请求参数
	 * @param type   分类类型1 讨论会 2 vod  3 voice
	 */
	public BusinessCategoryList(Context context,RequestParams params,int type) {
		super();
		this.context = context;
		this.params = params;
		aCache = ACache.get(context);
		progressHUD = ProgressHUD.show(context, context.getString(R.string.dataloding), false, true, null);
		catgoryBeans = new ArrayList<BusinessCategoryBean>();
		this.type = type;
		
		
	}
	
	
	
	
	
	

	public BusinessCategoryList(Context context, RequestParams params,
			String userMethord) {
		super();
		this.context = context;
		this.params = params;
		this.userMethord = userMethord;
		type = 400;
		aCache = ACache.get(context);
		progressHUD = ProgressHUD.show(context, context.getString(R.string.dataloding), false, true, null);
		catgoryBeans = new ArrayList<BusinessCategoryBean>();
		
		CATEGORY_HTTP_REQUEST = this.userMethord;
		
		
		
	}
	public List<BusinessCategoryBean> getAllCategory(){
		
		
		if(Toolkit.isAvaliable(context)){

			//有网络请求数据
			new AsyncHttpClient().post(CATEGORY_HTTP_REQUEST, params, new AsyncHttpResponseHandler(){
				
				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();
					
					progressHUD.show();
					
				}
				
				
				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					// TODO Auto-generated method stub
					super.onSuccess(arg0, arg1, arg2);
					
					String requestString  = new String(arg2);
					
					if(requestString!=null){
						aCache.put(CATEGORY_HTTP_REQUEST+params, requestString,App.CACHE_TIME_DAY_VERY_SHORT);
						setListData(requestString);
					}
					
					
				}
				
				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
					// TODO Auto-generated method stub
					super.onFailure(arg0, arg1, arg2, arg3);
					if(progressHUD.isShowing()){
						progressHUD.cancel();
					}
					
					Toast.makeText(context, context.getString(R.string.serverError), 0).show();
				}
				
				
				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					if(progressHUD.isShowing()){
						progressHUD.cancel();
					}
				}
			});
		
		}else{

			
			//没有网络就离线缓存

			requestString = aCache.getAsJSONObject(CATEGORY_HTTP_REQUEST+params).toString();
			
			if(requestString!=null){
				
				 setListData(requestString);
			}else{
				Toast.makeText(context, context.getString(R.string.noData), 0).show();
			}
			
		
		}
		
		
		
		return catgoryBeans;
	}
	
	private List<BusinessCategoryBean>  setListData(String arg2){
		
		String requestString = arg2;

		AllBusinessRequestHttpData requestBeanRequest = new AllBusinessRequestHttpData();


		if(requestString!=null){
			
			requestBeanRequest = new Gson().fromJson(requestString, new TypeToken<AllBusinessRequestHttpData>(){}.getType());

			if(requestBeanRequest.getState()==200){
				
				
				catgoryBeans = requestBeanRequest.getObjlistJson();
				
				Intent intent = new Intent(VodSearchActivity.RECEIVER_VODSEARCHACTIVITY);
				
				intent.putExtra("AllBusinessCategoryLisyBean", (Serializable)catgoryBeans);
				
				intent.putExtra("mark", type);
				
				context.sendBroadcast(intent);
				
				return requestBeanRequest.getObjlistJson();

			}

		}
		
		return null;
		
	}
	



	
	class AllBusinessRequestHttpData{
		private int state;

		private String message;

		private List<BusinessCategoryBean> objlistJson;

		public int getState() {
			return state;
		}

		public void setState(int state) {
			this.state = state;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public List<BusinessCategoryBean> getObjlistJson() {
			return objlistJson;
		}

		public void setObjlistJson(List<BusinessCategoryBean> objlistJson) {
			this.objlistJson = objlistJson;
		}


	}


	
}